
module.exports = function (source) {
  // ["transitionend", "animationiteration", "touchforcechange", "columnchange", "linechange", "error"] 没有测试过的事件
  const eventArr = ["share", "load", "transitionend", "animationstart", "animationiteration", "animationend", "touchforcechange", "animationfinish", "getuserinfo", "click", "touchstart", "touchmove", "touchcancel", "touchend", "tap", "longtap", "longpress", "input", "change", "submit", "blur", "focus", "reset", "confirm", "columnchange", "linechange", "error", "scrolltoupper", "scrolltolower", "scroll"]
  const reg = /(\@|v-on\s*:\s*)(share|load|transitionend|animationstart|animationiteration|animationend|touchforcechange|animationfinish|getuserinfo|click|touchstart|touchmove|touchcancel|touchend|tap|longtap|longpress|input|change|submit|blur|focus|reset|confirm|columnchange|linechange|error|scrolltoupper|scrolltolower|scroll)(\.[a-z]+)*\s*=\s*(\"|\')([a-zA-Z0-9_-~!#$%^&*()+={}<>?/,.\'\"]*)(\"|\'|\()/g
  // 获取元素绑定的事件数据 格式 ['@click="bindViewTap"', '@blur="clickHandle"']
  const onEventArr = source.match(reg)

  // console.log(onEventArr)

  if (onEventArr != null) {
    // 遍历事件数组 添加 data- 属性
    onEventArr.forEach(item => {
      // @blur="clickHandle" 分离绑定的事件 事件名
      const arr = item.split('=')
      const event = eventArr.find(eventItem => arr[0].indexOf(eventItem) > -1)
      let methodName = arr[1].trim()
      // 如果有参数只获取事件名
      if (methodName.indexOf('(') > -1) {
        methodName = methodName.substring(0, methodName.indexOf('(')) + methodName.substr(0, 1)
      }

      // 拼接 data- 属性  如: data-click="clickHandle"
      const attrStr = `data-${event}=${methodName}`

      // 为绑定事件特殊字符添加转义符 如: \@blur\=\"clickHandle\(\'123\'\,\ \$event\)\"
      let formatItem = item.split('')
      formatItem = formatItem.map(ele => {
        var pattern = new RegExp("[`~!@#$^&*()=|{}':;'\",\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ ]")
        if (pattern.test(ele)) ele = `\\${ele}`
        return ele
      })

      // @click="clickHandle" 替换成  data-click="clickHandle" @click="clickHandle"
      const eventReg = new RegExp(formatItem.join(''), 'g')
      source = source.replace(eventReg, `${attrStr} ${item}`)
      // 替换重复的 data- 属性
      const reAttrReg = new RegExp(`${attrStr} ${attrStr}`, 'g')
      source = source.replace(reAttrReg, `${attrStr}`)
    })
  }
  // console.log(source)
  return source
}
